package com.atjinggu.jinggumail.order.config;

import com.atjinggu.jinggumail.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jingming
 * @Type RabbitMqConfig.java
 * @Desc
 * @date 2021/3/16 16:22
 */
@Configuration
public class RabbitMqConfig {

    /**
     * 容器中的 Binding、Queue、Exchange 都会自动创建（RabbitMQ中没有的情况）
     * @return
     */
    @Bean
    public Queue orderDelayQueue(){
        //设置参数为死信队列
        Map<String,Object> args = new HashMap();
        args.put("x-dead-letter-exchange","order-event-exchange");
        args.put("x-dead-letter-routing-key","order.release.order");
        args.put("x-message-ttl",60000);
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        Queue queue = new Queue("order.delay.queue",true,false,false,args);
        return queue;
     }

     @Bean
    public Queue orderReleaseOrderQueue(){
         Queue queue = new Queue("order.release.order.queue",true,false,false);
         return queue;
    }

    @Bean
    public Exchange orderEventExchange(){
        TopicExchange topicExchange = new TopicExchange("order-event-exchange", true, false);
        return topicExchange;
    }

    @Bean
    public Binding orderCreateOrderBinding(){
        //String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments
       return new Binding("order.delay.queue",
               Binding.DestinationType.QUEUE,
               "order-event-exchange",
               "order.create.order",
               null);
    }

    @Bean
    public Binding orderReleaseOrderBinding(){
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    @Bean
    public Binding orderReleaseOtherBinding(){
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }

    @Bean
    public Queue orderSeckillOrderQueue(){
        /**
         * String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
         */
        return new Queue("order-seckill-order-queue",true,false,false);
    }

    @Bean
    public Binding orderSeckillBing(){
        /**
         * String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments) {
         */
        return new Binding("order-seckill-order-queue",
                Binding.DestinationType.QUEUE,"order-event-exchange","order.seckill.order",null);
    }

//    @RabbitHandler
//    public void listen(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
//        System.out.println("接收到 orderEntity..........."+orderEntity.getOrderSn());
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//    }

   }